package com.sc1.service.impl;

import com.sc1.mapper.HUserMapper;
import com.sc1.mapper.HdeptMapper;
import com.sc1.mapper.HuserinfoMapper;
import com.sc1.pojo.HUser;
import com.sc1.pojo.Hdept;
import com.sc1.service.HUserService;
import com.sc1.util.MybatisUtil;

import java.util.List;

public class HUserServiceImpl implements HUserService {
    @Override
    public List<HUser> show() {
        HUserMapper HUMapper = MybatisUtil.getMapper(HUserMapper.class);
        List<HUser> HUlist = HUMapper.select();
        MybatisUtil.close();
        return HUlist;
    }

    @Override
    public int insert(HUser HU) {
        HUserMapper HUMapper = MybatisUtil.getMapper(HUserMapper.class);
        int result = HUMapper.insert(HU);
        MybatisUtil.close(); // 提交事务
        HuserinfoMapper HUInfoMapper = MybatisUtil.getMapper(HuserinfoMapper.class);
        HU.getinfo().setId(HU.getId());
        HUInfoMapper.insert(HU.getinfo());
        MybatisUtil.close();
        return result;
    }

    @Override
    public int deleteInBulk(Integer[] ids) {
        HUserMapper HUMapper = MybatisUtil.getMapper(HUserMapper.class);
        int result = HUMapper.deleteInBulk(ids);
        MybatisUtil.close();
        return result;
    }

    @Override
    public List<Hdept> ShowDept(){
        HdeptMapper hdeptMapper = MybatisUtil.getMapper(HdeptMapper.class);
        List<Hdept> depts= hdeptMapper.selectAll();
        MybatisUtil.close();
        return depts;
    }

    @Override
    public void deleteDeptById(Integer id){
        HUserMapper hUserMapper =MybatisUtil.getMapper(HUserMapper.class);
        HUser u = new HUser();
        u.setId(id);
        hUserMapper.update(u);
        MybatisUtil.close();
        HdeptMapper hdeptMapper = MybatisUtil.getMapper(HdeptMapper.class);
        hdeptMapper.deleteByPrimaryKey(id);
        MybatisUtil.close();
    }
}
